package project3cs424.src;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class WeatherScraper {
	
	public static void main(String args[])throws IOException
	{
		File file=new File("project3cs424/weather/stations.txt");
		
		BufferedReader inputReader=new BufferedReader(new FileReader(file));
		BufferedWriter outputWriter=new BufferedWriter(new FileWriter(new File("data/weather.data")));
		while(inputReader.ready())
		{
			
			String inputLine=inputReader.readLine();
			//System.out.println(inputLine);
			//System.out.println(inputLine.length());
			if(inputLine.length()==83)
			{
				String state=inputLine.substring(0,2);
				String stationName=inputLine.substring(3,19);
				String latDeg=inputLine.substring(39,41);
				String latMin=inputLine.substring(42,46);
				String lngDeg=inputLine.substring(47,51);
				String lngMin=inputLine.substring(51,55);
				String latDir=latMin.substring(latMin.length()-2);
				String lngDir=lngMin.substring(lngMin.length()-2);
				float latMinFloat=Float.parseFloat(latMin.substring(0, latMin.length()-2));
				float lngMinFloat=Float.parseFloat(lngMin.substring(0,lngMin.length()-2));
				
				
				String country=inputLine.substring(inputLine.length()-2, inputLine.length());
				if(country.equals("US"))
				{
					System.out.println(lngDir);
					float latitude;
					float longitude;
					if(latDir.equals("N"))
					{
						latitude=(Float.parseFloat(latDeg)+(latMinFloat/60))*(-1);
					}
					else
					{
						latitude=(Float.parseFloat(latDeg)+(latMinFloat/60));
					}
					
					if(lngDir.equals("E"))
					{
						longitude=Float.parseFloat(lngDeg)+(lngMinFloat/60);
					}
					else
					{
						longitude=(Float.parseFloat(lngDeg)+(lngMinFloat/60))*(-1);
					}
					
					outputWriter.write(stationName+"\t"+state+"\t"+latitude+"\t"+longitude+"\t"+country+"\n");
				}	
			}
			
			
		}
		outputWriter.close();
	}

}
